<template>
  <ElCard
    v-loading.fullscreen.lock="fullscreenLoading"
    element-loading-background="rgba(0, 0, 0, 0.35)"
    :class="s.baby"
  >
    <div class="one">
      <div class="child">
        <span class="must-check">* </span>姓名：
        <ElSelect
          v-model="child"
          allow-create
          filterable
          class="w120"
          placeholder="请选择"
          @blur="ll"
        >
          <ElOption
            v-for="item in children"
            :key="item.id"
            :label="item.child_name"
            :value="item.id"
          >
          </ElOption>
        </ElSelect>
      </div>
      <template v-if="childInfo">
        <div class="child">年龄：{{ childInfo.birthday_text }}</div>
        <div class="child">性别：{{ childInfo.sex_text }}</div>
        <div class="child">电话：{{ childInfo.parent_tel }}</div>
      </template>
      <template v-else>
        <div class="child">
          <span class="must-check">* </span>生日：
          <ElDatePicker
            v-model="birthday"
            type="date"
            prefix-icon="dd"
            class="w120"
            placeholder=""
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :picker-options="pickOptions"
            @change="changeEditStatus(true)"
          >
          </ElDatePicker>
        </div>
        <div class="child">
          <span class="must-check">* </span>性别：
          <ElRadioGroup v-model="sex" @change="changeEditStatus(true)">
            <ElRadioButton label="男"></ElRadioButton>
            <ElRadioButton label="女"></ElRadioButton>
          </ElRadioGroup>
        </div>
        <div class="child">
          <span class="must-check no-content">* </span>电话：
          <ElInput
            v-model="phone"
            class="float-right w120"
            placeholder=""
            @change="changeEditStatus(true)"
          ></ElInput>
        </div>
      </template>
    </div>
    <div v-if="childInfo" class="two">
      <div class="child">身高：{{ childInfo.height || '无' }}</div>
      <div class="child">体重：{{ childInfo.weight || '无' }}</div>
      <div class="child">体质：{{ childInfo.body_name || '无' }}</div>
      <div class="child">上次测试时间：{{ childInfo.last_time || '无' }}</div>
    </div>
  </ElCard>
</template>

<script>
import _ from 'lodash';
import {http, catchAppError} from '@/util';
import {menusNavigation} from '@/store';
import url from '@/config';
import toast from '@/toast';

export default {
  name: 'BabySearch',
  model: {
    prop: 'babyInfo',
    event: 'changeBabyInfo'
  },
  props: {
    babyInfo: {
      type: Object,
      default: () => ({})
    },
    tabName: {
      type: String,
      default: 'Evaluation'
    },
    babyId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      listChildLoading: true,
      babySearLoading: false,
      id: '0',
      child: null,
      birthday: '',
      sex: '男',
      children: [],
      phone: '',
      pickOptions: {
        disabledDate(time) {
          return (
            time.getTime() > Date.now() ||
            time.getTime() < Date.now() - 1000 * 60 * 60 * 24 * 365 * 18
          );
        }
      }
    };
  },
  computed: {
    childInfo() {
      return this.children.find(child => child.id === this.child);
    },
    fullscreenLoading() {
      return this.listChildLoading || this.babySearLoading;
    }
  },
  watch: {
    child() {
      const value = this.childInfo;
      if (value) {
        this.$emit('changeBabyInfo', value);
        return;
      }

      this.$emit('changeBabyInfo', {
        child_name: this.child,
        birthday: this.birthday,
        sex: this.sex,
        tel: this.phone
      });
    },
    babyInfo(value) {
      if (value.isClear) {
        this.clearData();
      }
    },
    babyId: {
      handler() {
        if (+this.babyId && this.children.length) {
          this.babySearLoading = true;
          this.searchBabyById();
        }
      },
      immediate: true
    }
  },
  beforeMount() {
    http
      .post(url.testChildList)
      .then(res => {
        this.children = res;
        this.listChildLoading = false;
        if (+this.babyId) {
          this.babySearLoading = true;
          this.searchBabyById();
        }
      })
      .catch(catchAppError);
  },
  methods: {
    ll(event) {
      this.child = event.srcElement.value || null;
    },
    clearData() {
      this.id = '';
      this.child = null;
      this.birthday = '';
      this.sex = 1;
      this.phone = '';
    },
    changeEditStatus(status = true) {
      const tab = menusNavigation.tabs.find(tab => tab.tab === this.tabName);
      tab.isEditing = status;
      this.$emit('changeBabyInfo', {
        child_name: this.child,
        birthday: this.birthday,
        sex: this.sex,
        tel: this.phone
      });
    },
    searchBabyById() {
      http
        .post(url.testChildList, {key: this.babyId})
        .then(res => {
          this.child = res[0].id;
          this.$emit('changeBabyInfo', {...res[0]});
          this.babySearLoading = false;
        })
        .catch(catchAppError);
    }
  }
};
</script>

<style lang="scss" module="s">
.baby {
  :global {
    width: 100%;
    font-size: 16px;
    .w120 {
      width: 120px;
    }
    .el-card__body .two {
      margin-top: 30px;
    }
    .el-card__body .one,
    .el-card__body .two {
      &.with-tips {
        justify-content: space-between;
      }
      display: flex;
      align-items: center;
      .child {
        margin-right: 20px;
        width: 300px;
        .item-text {
          color: #4b90ff;
        }
        .el-input__inner {
          border-color: #4b90ff;
          color: #4b90ff;
          padding-left: 15px;
          padding-right: 15px;
        }
        .item-icon {
          width: 12px;
          height: 12px;
          background-color: #4b90ff;
          display: inline-block;
          border-radius: 50%;
          margin-bottom: 3px;
          margin-right: 5px;
          vertical-align: middle;
        }
        .el-radio-button {
          margin-right: 10px;
          .el-radio-button__inner {
            border-left: 1px solid #4b90ff;
            border: 1px solid #4b90ff;
            color: #4b90ff;
            border-radius: 20px;
            padding: 7px 15px;
            font-size: 12px;
          }
          .el-radio-button__orig-radio:checked + .el-radio-button__inner {
            background-color: #ffb03e;
            border-left: 1px solid #ffb03e;
            border: 1px solid #ffb03e;
            box-shadow: -1px 0 0 0 #ffb03e;
            color: #fff;
          }
        }
      }
    }
    .float-right {
      vertical-align: middle;
    }
  }
}
</style>
